/**
 * Created by Administrator on 2017/5/23.
 */
'use strict'
var resultTemplete = require('html!./show-setting.html');
require('./showSetting.less');
require('./model.less');
module.exports = {
  template: resultTemplete,
  data:function(){
    return {
      lay:'',
      layLoad:'',
     modelId:localStorage.getItem("modelId"),
      resultList:[],//算法结果列表
      fieldList:[],//变量列表
      oldTableName:localStorage.getItem("oldTableName"), //数据清单需要的表名
      resultTableName:localStorage.getItem("oldTableName"),//结果所需要的表名
      add_imgName:'',
      add_xColumn:'',
      add_xExpress:$("#add_xSelected").find("option:selected").text(),
      add_yColumn:'',
      add_yValue:'',
      add_yExpress:$("#add_ySelected").find("option:selected").text(),
      add_imgId:'',
      settingList:[],
      edit_imgName:'',
      edit_xColumn:'',
      edit_xExpress:$("#edit_xSelected").find("option:selected").text(),
      edit_yColumn:'',
      edit_yValue:'',
      edit_yExpress:$("#edit_ySelected").find("option:selected").text(),
      edit_imgId:'',
      edit_id:'',
      editlay:''
    }
  },
  created:function(){
    var me = this;
    me.layLoad = layer.load(2, {shade: 0.1, area: ['32px', '64px']});
    me.getDataList();
  },
  methods:{
    /**
     * 执行算法
     */
    getDataList:function () {
      var me = this;
      this.$http.get('/eyeShowResultController/getDataList.do?modelId='+me.modelId+'&tableName='+me.oldTableName).then(function(res) {
        //获取变量列表
        if(res.data.status === 'OK') {
          console.log("执行中");
          //定时获取算法状态
          var timeout = setInterval(function(){
            if(me.resultList.length!=0){
             // layer.close(me.layLoad);
              clearInterval(timeout);
            }else{
              me.getState();
            }
          },30000);
        }
      });
    },
    getState:function () {
      var me = this;
      this.$http.get('/eyeShowResultController/getState.do?tableName='+me.oldTableName).then(function(res) {
        //获取变量列表
        if(res.data.status === 'OK') {
          if(res.data.result.length!=0){
            me.resultList=res.data.result;
            //获取变量名称
            me.getFiledList();
            //获取图形设置列表
            me.getAllSettings();
          }
        }
      });
    },
    getAllSettings:function () {
      var me = this;
      this.$http.get('/eyeImgSettingController/selectAllSetting.do?modelId='+me.modelId).then(function(res) {
        //获取变量列表
        if(res.data.status === 'OK') {
          me.settingList = res.data.result;
          layer.close(me.layLoad);
        }else{
          layer.close(me.layLoad);
          layer.msg("获取列表失败");
        }
      });
    },
    del:function (event,id) {
      var me = this;
      var that = $(event.currentTarget);
      var deleteLay = layer.confirm('是否确认删除此图形设置？', {
          btn: ['是', '否'] //按钮
        }, function () {
        me.$http.get('/eyeImgSettingController/deleteSetting.do?id=' + id).then(function (res) {
          //获取变量列表
          if (res.data.status === 'OK') {
            layer.msg(res.data.result);
            that.parents('.img-block').remove();
            //me.getAllSettings();
          } else {
            layer.msg(res.data.result);
          }
        });
      });
    },
    edit:function () {
      var me = this;
      if(me.edit_imgId==''){
        layer.msg("请选择图形");
        return;
      }
      if(me.edit_imgName==''){
        layer.msg("图形名称不能为空");
        return;
      }
      if(me.edit_xColumn==''){
        layer.msg("x变量不能为空");
        return;
      }
      if(me.edit_yColumn==''){
        layer.msg("y变量不能为空");
        return;
      }
      if(me.edit_yValue==''){
        layer.msg("目标不能为空");
        return;
      }
      this.$http.get('/eyeImgSettingController/updateSetting.do?imgId='+me.edit_imgId+'&name='+me.edit_imgName+'&xColumn='+me.edit_xColumn+'&xExpress='+$("#edit_xSelected").find("option:selected").text()+'&yColumn='+me.edit_yColumn+'&yValue='+me.edit_yValue+'&yExpress='+$("#edit_ySelected").find("option:selected").text()+'&modelId='+me.modelId+'&id='+me.edit_id).then(function(res) {
        //获取变量列表
        if(res.data.status === 'OK') {
          layer.msg(res.data.result);
          me.getAllSettings();
          me.closeEditLay();
        }else{
          layer.msg(res.data.message);
        }
      });
    },
    detail:function (event,id) {
      var me = this;
      var that = $(event.currentTarget);
      me.$http.get('/eyeImgSettingController/detail.do?id=' + id).then(function (res) {
          //获取变量列表
          if (res.data.status === 'OK') {
            var map = res.data.result;
            me.edit_id = id;
            me.edit_imgName = map.name;
            me.edit_xColumn = map.xColumn;
            me.edit_yColumn = map.yColumn;
            me.edit_yValue = map.yValue;
            me.edit_imgId  = map.imgId;
            me.editOpenLayer();
          } else {
            layer.msg(res.data.msg);
          }
        });
    },
    add:function(){
      var me = this;
      if(me.add_imgId==''){
        layer.msg("请选择图形");
        return;
      }
      if(me.add_imgName==''){
        layer.msg("图形名称不能为空");
        return;
      }
      if(me.add_xColumn==''){
        layer.msg("x变量不能为空");
        return;
      }
      if(me.add_yColumn==''){
        layer.msg("y变量不能为空");
        return;
      }
      if(me.add_yValue==''){
        layer.msg("目标不能为空");
        return;
      }
      this.$http.get('/eyeImgSettingController/addSetting.do?imgId='+me.add_imgId+'&name='+me.add_imgName+'&xColumn='+me.add_xColumn+'&xExpress='+$("#add_xSelected").find("option:selected").text()+'&yColumn='+me.add_yColumn+'&yValue='+me.add_yValue+'&yExpress='+$("#add_ySelected").find("option:selected").text()+'&modelId='+me.modelId).then(function(res) {
        //获取变量列表
        if(res.data.status === 'OK') {
          layer.msg(res.data.result);
          me.getAllSettings();
          me.closeLay();
          me.add_imgName = '';
          me.add_xColumn = '';
          me.add_yColumn = '';
          me.add_yValue = '';
          me.add_imgId = '';
        }else{
          layer.msg(res.data.message);
          me.add_imgName = '';
          me.add_xColumn = '';
          me.add_yColumn = '';
          me.add_yValue = '';
          me.add_imgId = '';
        }
      });
    },
    getFiledList:function(){
      var me = this;
      this.$http.get('/eyeShowResultController/getValue.do?tableName='+me.resultTableName).then(function(res) {
        //获取变量列表
          if(res.data.status === 'OK') {
            me.fieldList = res.data.result;
            layer.close(me.layLoad);
          }else{
            layer.close(me.layLoad);
            layer.msg("获取失败");

          }
      });
    },
    editOpenLayer:function () {
      this.editlay = layer.open({
        type: 1,
        title: false,
        /*offset: '200px', //具体配置参考：offset参数项*/
        content: $('#edit'),
        closeBtn:0,
        area: ['500px', 'auto'],
        shade: 0 //不显示遮罩
      });
    },
    closeEditLay:function(){
      var me = this;
      layer.close(me.editlay);
    },
    addImgSetting:function(){
     this.lay = layer.open({
        type: 1,
        title: false,
       /* offset: '200px', //具体配置参考：offset参数项*/
        content: $('#add'),
        closeBtn:0,
        area: ['500px','auto'],
        shade: 0 //不显示遮罩
      });
    },
    closeLay:function(){
      var me = this;
      layer.close(me.lay);
      me.add_imgName = '';
      me.add_xColumn = '';
      me.add_yColumn = '';
      me.add_yValue = '';
      me.add_imgId = '';
    },
    goback:function(){
      var me = this;
      this.$router.replace({ path: '/product/inputSet',query:{modelId:me.modelId}});
    },
    next:function(){
      var me = this;
      localStorage.setItem('modelId',me.modelId);
      localStorage.setItem('tableName',me.resultTableName);
      me.$router.push('/product/result');
      //生成分析数据
      //this.$router.push({ path:'/product/result', params: { modelId: me.modelId,tableName:me.resultTableName }})
    }
  }
};
